package com.heluo.eduservice.controller;


import com.heluo.commonutils.R;
import com.heluo.eduservice.client.VodClient;
import com.heluo.eduservice.entity.EduVideo;
import com.heluo.eduservice.service.EduVideoService;
import com.heluo.servicebase.exceptionhandler.HeluoException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-10
 */
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }
    @DeleteMapping("{videoId}")
    public R deleteVideo(@PathVariable String videoId){
        EduVideo byId = videoService.getById(videoId);
        String videoSourceId=byId.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            R r = vodClient.removeAlyVideo(videoSourceId);
            if (r.getCode()==20001){
                throw new HeluoException(20001,"删除视频失败");
            }
        }
        videoService.removeById(videoId);
        return R.ok();
    }
    @PutMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();
    }
    @GetMapping("selectVideo/{videoId}")
    public R selectVideo(@PathVariable String videoId){

        EduVideo video = videoService.getById(videoId);
        return R.ok().data("data",video);
    }
}

